<?php

require_once 'BaseTaxonomy.php';
class ListTaxonomy extends BaseTaxonomy
{
    public $type = 'dropdown'; //checkbox,radio
    public $repeatChar = '--';
    public $useRepeatChar = true;
    public $exclude = array();

    private $_data;

    public function run()
    {
        switch (strtolower($this->type)) {
            case 'dropdown':
                $selected = $this->getSelected();
                if (is_array($selected)) {
                    if (count($selected)) {
                        $selected = array_shift($selected);
                    }
                    else {
                        $selected = '';
                    }
                }
                if (is_object($this->model)) {
                    $this->model->{$this->attribute} = $selected;
                    echo CHtml::activeDropDownList($this->model, $this->attribute, $this->getData(), $this->htmlOptions);
                }
                else
                    echo CHtml::dropDownList($this->name, $selected, $this->getData(), $this->htmlOptions);
                break;
            case 'radio':
                $selected = $this->getSelected();
                if (is_array($selected)) {
                    if (count($selected)) {
                        $selected = array_shift($selected);
                    }
                    else {
                        $selected = '';
                    }
                }
                if (is_object($this->model)) {
                    $this->model->{$this->attribute} = $selected;
                    echo CHtml::activeRadioButtonList($this->model, $this->attribute, $this->getData(), $this->htmlOptions);
                }
                else
                    echo CHtml::radioButtonList($this->name, $selected, $this->getData(), $this->htmlOptions);
                break;
            case 'checkbox':
                if (is_object($this->model)) {
                    $this->model->{$this->attribute} = $this->getSelected();
                    echo CHtml::activeCheckBoxList($this->model, $this->attribute, $this->getData(), $this->htmlOptions);
                }
                else
                    echo CHtml::checkBoxList($this->name, $this->getSelected(), $this->getData(), $this->htmlOptions);
                break;
        }
    }

    public function getData()
    {
        $result = array();
        $data = parent::getData();
        if (is_array($data) && count($data)) {
            foreach ($data as $item) {
                $result = $this->prepareData($item);
            }
        }
        return $result;
    }

    protected function prepareData($parent)
    {
        if ($this->_data === null) {
            $this->_data = array();
        }
        if (count($this->exclude) && in_array($parent['id'], $this->exclude)) return $this->_data;
        $this->_data[$parent['id']] = ($this->useRepeatChar ? str_repeat($this->repeatChar, $parent['level']) : '') . $parent['name'];
        if (isset($parent['children']) && is_array($parent['children']) && count($parent['children'])) {
            foreach ($parent['children'] as $child) {
                $this->prepareData($child);
            }
        }
        return $this->_data;
    }
}